// JavaScript Document
var http = require('http')
	, fs = require('fs')
	, mongoose = require('mongoose');

var webserver  = http.createServer( function (req, res) {
	fs.readFile('fog1.2.2.html', 'utf8', function (err, data) {
		res.writeHead(200, { 'Content-Type': 'text/html' });
		res.end(data);
	});
});


var Schema = mongoose.Schema;

var GridSchema = new Schema({
	_id : String,
	gridx : Number,
	gridy : Number,
	writime : Number,
	color : String,
	class : String,
	//subject : String,
	keyword : String,
	key_id: String,
	content : String,
	//good : Number,
	//bad : Number,
	nearby : [Boolean],
	relative : [Boolean]
});

var db = mongoose.createConnection('localhost', 'test')
	, GridModel = db.model('fullopengrid', GridSchema)
	, GridData = db.model('fullopengrid');

//app.get

var io = require('socket.io').listen(webserver);
io.set('log level', 1);

//var color, keyword, content;

var gridid;
var keyid;

io.sockets.on('connection', function (socket) {
	socket.on('ajaj', function (data) {
		
		GridData.find()
		.where('gridx').gte(data.cfx-64).lt(data.cfx+64)
		.where('gridy').gte(data.cfy-24).lt(data.cfy+24)
		.where('class').in(['filled', 'relaempty'])
		.exec(function (err, gridb){
			socket.emit('ajaj', gridb);
		});
	});
	socket.on('regist', function (data) {
		//if (data.class == 'empty') {
		var mainIdb;
		console.log(data);
		if (data.key_id == null) {
			keyid = Number(new Date()) + (Math.floor(Math.random()*1000));
		}
		else if (data.key_id != null) {
			keyid = data.key_id;
		}
		GridData.findById(data.id, function(err, gridb) {
			gridb.relative[0] = 1;
			gridb.color = data.fill;
			gridb.keyword = data.keyword;
			gridb.key_id = keyid;
			gridb.content = data.message;
			gridb.class = 'filled';
			gridb.writime = Number(new Date());
			gridb.markModified('relative');
			gridb.markModified('nearby');
			gridb.save();
			io.sockets.emit('registpush', gridb);	
		});		
		GridData.findById(data.id1, function(err, gridb) {
			if (gridb.class == null) {
				gridb.relative[4] = 1;
				gridb.nearby[4] = 1;
				gridb.class = 'relaempty';
				gridb.key_id = keyid;
				gridb.keyword = data.keyword
				gridb.color = data.fill;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.class == 'relaempty' &&  gridb.key_id == keyid) {
				
				gridb.relative[4] = 1;
				gridb.nearby[4] = 1;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
			}
			else {
				gridb.nearby[4] = 1;
				gridb.markModified('nearby');
				gridb.save();
			}
		});		
		GridData.findById(data.id2, function(err, gridb) {
			if (gridb.class == null) {
				
				gridb.relative[5] = 1;
				gridb.nearby[5] = 1;
				gridb.class = 'relaempty';
				gridb.key_id = keyid;
				gridb.keyword = data.keyword;
				gridb.color = data.fill;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.class == 'relaempty' &&  gridb.key_id == keyid) {
				
				gridb.relative[5] = 1;
				gridb.nearby[5] = 1;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
			}
			else {
				gridb.nearby[5] = 1;
				gridb.markModified('nearby');
				gridb.save();
			}
		});
		GridData.findById(data.id3, function(err, gridb) {
			if (gridb.class == null) {
				
				gridb.relative[6] = 1;
				gridb.nearby[6] = 1;
				gridb.class = 'relaempty';
				gridb.key_id = keyid;
				gridb.keyword = data.keyword;
				gridb.color = data.fill;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.class == 'relaempty' &&  gridb.key_id == keyid) {
				
				gridb.relative[6] = 1;
				gridb.nearby[6] = 1;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
			}
			else {
				gridb.nearby[6] = 1;
				gridb.save();
			}
		});
		GridData.findById(data.id4, function(err, gridb) {
			if (gridb.class == null) {
				
				gridb.relative[1] = 1;
				gridb.nearby[1] = 1;
				gridb.class = 'relaempty';
				gridb.key_id = keyid;
				gridb.keyword = data.keyword;
				gridb.color = data.fill;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.class == 'relaempty' &&  gridb.key_id == keyid) {
				
				gridb.relative[1] = 1;
				gridb.nearby[1] = 1;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
			}
			else {
				gridb.nearby[1] = 1;
				gridb.markModified('nearby');
				gridb.save();
			}
		});
		GridData.findById(data.id5, function(err, gridb) {
			if (gridb.class == null) {
				
				gridb.relative[2] = 1;
				gridb.nearby[2] = 1;
				gridb.class = 'relaempty';
				gridb.key_id = keyid;
				gridb.keyword = data.keyword;
				gridb.color = data.fill;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.class == 'relaempty' &&  gridb.key_id == keyid) {
				
				gridb.relative[2] = 1;
				gridb.nearby[2] = 1;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
			}
			else {
				gridb.nearby[2] = 1;
				gridb.save();
			}
		});
		GridData.findById(data.id6, function(err, gridb) {
			if (gridb.class == null) {
				
				gridb.relative[3] = 1;
				gridb.nearby[3] = 1;
				gridb.class = 'relaempty';
				gridb.key_id = keyid;
				gridb.keyword = data.keyword;
				gridb.color = data.fill;
				gridb.markModified('relative');
				gridb.save();
				io.sockets.emit('registpushN', gridb);
			}
			else if (gridb.class == 'relaempty' &&  gridb.key_id == keyid) {
				
				gridb.relative[3] = 1;
				gridb.nearby[3] = 1;
				gridb.markModified('relative');
				gridb.markModified('nearby');
				gridb.save();
			}
			else {
				gridb.nearby[3] = 1;
				gridb.markModified('nearby');
				gridb.save();
			}
		});
		
	});
});

webserver.listen(7777);